package com.jt.www.common.controller;

import com.github.pagehelper.Page;
import com.jt.www.admin.user.service.UserService;
import com.jt.www.common.service.CorrectService;
import com.jt.www.domain.enums.common.YesOrNoEnum;
import com.jt.www.model.correct.ApproveHistoryEntity;
import com.jt.www.model.correct.CorrectRecordEntity;
import com.jt.www.model.correct.vo.ApproveParam;
import com.jt.www.model.correct.vo.CorrectionVoucherStatusVO;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.model.torectify.vo.CorrectListParam;
import com.jt.www.model.torectify.vo.IntertemporalParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author xq.wu03
 * @Classname ToRectifyController
 * @Description 冲正功能
 * @create 2020-06-14 11:13
 */
@Api(value = "冲正功能", tags = {"TORECTIFY-INFO-API"}, description = "保单/批单冲正接口")
@RestController
@RequestMapping(value = "toRectify")
public class ToRectifyController {

    public static final Logger log = LoggerFactory.getLogger(ToRectifyController.class);
    private static final String JTPF_USER_ID = "jtpf.userId";
    private static final String BRANCH_CODE = "branchCode";
    @Autowired
    CorrectService correctService;
    @Autowired
    UserService userService;

    @ApiOperation(value = "冲正列表", notes = "查看冲正数据列表", httpMethod = "POST")
    @PostMapping(value = "/v1/records", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericListResponse<List<CorrectRecordEntity>>> queryRecords(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @RequestBody CorrectListParam correctListParam, HttpServletRequest request) {
        correctListParam.setSysChannelCode(sysChannelCode);
        String findCode = "records_" + System.currentTimeMillis();
        String userCode = request.getHeader(JTPF_USER_ID);
        String branchCodes = userService.getBranchCodes(findCode, userCode);
        correctListParam.setBranchCode(branchCodes);
        if (StringUtils.isBlank(userCode)) {
            log.info("{} 无用户信息!", findCode);
            return GenericListResponse.listNoCount(null);
        }
        String approveNot = YesOrNoEnum.NO.getType();
        Page page = correctService.queryByCondition(correctListParam, approveNot);
        return GenericListResponse.listAndCount(page.getResult(), page.getTotal());
    }


    @ApiOperation(value = "冲正审批列表", notes = "查看冲正审批数据列表", httpMethod = "POST")
    @PostMapping(value = "/v1/records_examine", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericListResponse<List<CorrectRecordEntity>>> queryExamineRecords(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @RequestBody CorrectListParam correctListParam, HttpServletRequest request) {
        String findCode = "records_examine" + System.currentTimeMillis();
        correctListParam.setSysChannelCode(sysChannelCode);
        String approveNot = YesOrNoEnum.YES.getType();
        String userCode = request.getHeader(JTPF_USER_ID);
        String branchCodes = userService.getBranchCodes(findCode, userCode);
        correctListParam.setBranchCode(branchCodes);
        Page page = correctService.queryExamineByCondition(correctListParam, approveNot);
        return GenericListResponse.listAndCount(page.getResult(), page.getTotal());
    }


    @ApiOperation(value = "查询冲正是否跨期以及倒签", notes = "查询冲正是否跨期以及倒签", httpMethod = "POST")
    @PostMapping(value = "/isIntertemporal/{correctNo}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity isIntertemporal(@ApiParam(value = "冲正单号", required = true) @PathVariable String correctNo,
                                          @ApiParam(value = "请求参数", required = true) @RequestBody IntertemporalParam intertemporalParam)throws Exception {
        String findCode = "isIntertemporal" + System.currentTimeMillis();
        return correctService.isIntertemporal(findCode,correctNo,intertemporalParam );
    }

    @ApiOperation(value = "新增冲正", notes = "新增冲正内容", httpMethod = "POST")
    @PostMapping(value = "/v1/record", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericResponse> saveRecord(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @RequestHeader(value = "jtpf.userId", required = false)
            @ApiParam(value = "用户code") @NotEmpty(message = "用户code不能为空") String userCode,
            @RequestBody @ApiParam(name = "需要解析的字符串集合", value = "传入json格式", required = true) CorrectRecordEntity param, HttpServletRequest request) {
        param.setSysChannel(sysChannelCode);
        String branchCode = request.getHeader(BRANCH_CODE);
        param.setBranchCode(branchCode);
        correctService.pushCorrect(param);
        return GenericResponse.ok();
    }


    @ApiOperation(value = "修改冲正信息", notes = "修改冲正内容", httpMethod = "POST")
    @PostMapping(value = "/v1/updateRecord", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericResponse> updateRecord(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @RequestHeader(value = "jtpf.userId", required = false)
            @ApiParam(value = "用户code") @NotEmpty(message = "用户code不能为空") String userCode,
            @RequestBody @ApiParam(name = "需要解析的字符串集合", value = "传入json格式", required = true) CorrectRecordEntity param, HttpServletRequest request) {
        param.setSysChannel(sysChannelCode);
        String branchCode = request.getHeader(BRANCH_CODE);
        param.setBranchCode(branchCode);
        correctService.updateCorrect(param);
        return GenericResponse.ok();
    }


    @ApiOperation(value = "冲正详情", notes = "查看冲正详细数据", httpMethod = "GET")
    @GetMapping(value = "/v1/record/details/{id}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericDataResponse<CorrectRecordEntity>> queryRecord(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @NotNull(message = "冲正数据id不能为空")
            @ApiParam(value = "冲正数据id") @PathVariable(value = "id") Long id) {
        return GenericDataResponse.okWithData(correctService.queryById(id));
    }


    @ApiOperation(value = "根据id查看审批意见", notes = "根据id查看审批意见", httpMethod = "GET")
    @GetMapping(value = "/v1/record/details_Approval/{id}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericDataResponse<ApproveHistoryEntity>> queryApprovalRecord(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @NotNull(message = "审核意见id不能为空")
            @ApiParam(value = "审核意见id") @PathVariable(value = "id") Long id) {
        return GenericDataResponse.okWithData(correctService.queryApproveById(id));
    }

    @ApiOperation(value = "查询是否在审批中", notes = "查询是否在审批中", httpMethod = "GET")
    @GetMapping(value = "/v1/record/{correctNo}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Boolean queryRecord(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @NotNull(message = "投保单/批单不能为空")
            @ApiParam(value = "投保单/批单号") @PathVariable(value = "correctNo") String correctNo) {
        return correctService.isApproval(sysChannelCode, correctNo);
    }


    @ApiOperation(value = "查询该投保单是否可以冲正", notes = "查询该投保单是否可以冲正", httpMethod = "GET")
    @GetMapping(value = "/v1/record/applicationFormCoed/{appCode}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericResponse> isRecordAppCode(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @NotNull(message = "投保单号不能为空")
            @ApiParam(value = "投保单号不能为空") @PathVariable(value = "appCode") String appCode) {
        return correctService.isRecordAppCode(appCode);
    }


    @ApiOperation(value = "查询该投保单状态是否变更", notes = "查询该投保单状态是否变更", httpMethod = "GET")
    @GetMapping(value = "/v1/record/selectAppCodeStatus/{appCode}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericResponse> selectAppCodeStatus(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @NotNull(message = "投保单号不能为空")
            @ApiParam(value = "投保单号不能为空") @PathVariable(value = "appCode") String appCode) {
        return correctService.selectAppCodeStatus(appCode);
    }


    @ApiOperation(value = "冲正审核接口", notes = "对冲正请求进行审核", httpMethod = "POST")
    @PostMapping(value = "/v1/approve/{correctNoId}/{issueType}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<GenericResponse> approve(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @ApiParam(value = "审批的投保单/批单id") @PathVariable(value = "correctNoId") String correctNoId,
            @ApiParam(value = "出单方式(00线下/01线上)") @PathVariable(value = "issueType") String issueType,
            @RequestBody @ApiParam(name = "审核参数", value = "传入json格式", required = true) ApproveParam approveParam,
            @RequestHeader(value = "jtpf.userId", required = false) @ApiParam(value = "用户code") @NotEmpty(message = "用户code不能为空") String userCode) {

        correctService.approve(sysChannelCode, correctNoId, approveParam, issueType);
        return GenericDataResponse.ok();
    }

    @ApiOperation(value = "查询凭证接口", notes = "查询凭证接口", httpMethod = "GET")
    @GetMapping(value = "/v1/query/voucher/{correctNo}/{policyType}/{correctType}")
    public GenericListResponse<CorrectionVoucherStatusVO> queryRecord(
            @RequestHeader(value = "jtpf.sysCode", required = false)
            @ApiParam(value = "系统编码") @NotEmpty(message = "系统编码不能为空") String sysChannelCode,
            @NotNull(message = "投保单/批单不能为空")
            @ApiParam(value = "投保单/批单号") @PathVariable(value = "correctNo") String correctNo,
            @ApiParam(value = "冲正对象") @PathVariable("policyType") String policyType,
            @ApiParam(value = "冲正类型") @PathVariable("correctType") String correctType) {
        return correctService.queryVoucher(correctNo, policyType, correctType);
    }

}
